package repository

import (
	"context"
	"jk-webook/internal/domain"
	"jk-webook/internal/repository/cache"
	"jk-webook/internal/repository/dao"
)

type UserRepository struct {
	dao   *dao.UserDao
	cache *cache.UserCache
}

// NewUserRepository 类似于依赖注入的方式
func NewUserRepository(dao *dao.UserDao, userCache *cache.UserCache) *UserRepository {
	return &UserRepository{dao: dao, cache: userCache}
}

func (r *UserRepository) FindById(ctx context.Context, id int64) (domain.User, error) {
	u, err := r.cache.Get(ctx, id)
	if err == nil {
		return u, nil
	}
	//if err == cache.ErrKeyNotExists {
	//
	//}
	u, err = r.dao.FindById(ctx, id)
	if err != nil {
		return domain.User{}, err
	}
	err = r.cache.Set(ctx, u)
	return u, err
}

func (r *UserRepository) Create(ctx context.Context, u domain.User) error {
	return r.dao.Insert(ctx, dao.User{
		Email:    u.Email,
		Password: u.Password,
	})
}
func (r *UserRepository) domainToEntity(u domain.User) dao.User {
	return dao.User{}
}
func (r *UserRepository) entityToDomain(u dao.User) domain.User {
	return domain.User{}
}
